#!/bin/bash
set -e

# Get the IP address of this unit and the remote unit
peer_ip=$(relation-get private-address)
this_ip=$(unit-get private-address)

# Get the machine number of this unit and the remote unit. This
# takes the form '<charm name>/<index>
peer_index=$(echo $JUJU_REMOTE_UNIT | cut -d'/' -f2)
this_index=$(echo $JUJU_UNIT_NAME | cut -d'/' -f2)

# Re-create the node.json files in the data bags
$CHARM_DIR/lib/node_json_clustered_script --ip "$peer_ip" --index "$peer_index" --cluster
$CHARM_DIR/lib/node_json_clustered_script --ip "$this_ip" --index "$this_index" --cluster

# Re-create the node.json file and re-run chef sole
$CHARM_DIR/lib/node_json_script --cluster
chef-solo -c /home/ubuntu/chef-solo/solo.rb -j /home/ubuntu/chef-solo/node.json

# Re-create the node.json with the tag.
$CHARM_DIR/lib/node_json_script --cluster --tag
